﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Wpftest.CommonClass
{
    class TreeHelper
    {
        static string GetTypeDescription(object obj) {
            return obj.GetType().FullName;
        }
        //dobj是什么：一个依赖对象，就像一个树型结构的一个节点，形成树时，从根节点开始递归形成树；
        //获取逻辑树
        public static TreeViewItem GetLogicTree(DependencyObject dobj)
        {
            if (dobj == null)
            {
                return null;
            }
            var treeitem = new TreeViewItem { Header = GetTypeDescription(dobj),IsExpanded=true };
            foreach (var child in LogicalTreeHelper.GetChildren(dobj))
            {
                var item = GetLogicTree(child as DependencyObject);
                if (item != null)
                {
                    treeitem.Items.Add(item);
                }
            }
            return treeitem;
        }
        //获取视觉树
        public static TreeViewItem GetVisulTree(DependencyObject dobj)
        {
            if (dobj == null)
            {
                return null;
            }
            var treeitem = new TreeViewItem { Header = GetTypeDescription(dobj), IsExpanded = true };
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dobj); i++)
			{
                var child = VisualTreeHelper.GetChild(dobj, i);
                var item = GetVisulTree(child);
                if (item != null)
                {
                    treeitem.Items.Add(item);
                }
            }
            return treeitem;
        }
    }
}
